package com.atyoung.youngmall.product.service.impl;

import com.atyoung.youngmall.product.dao.AttrAttrgroupRelationDao;
import com.atyoung.youngmall.product.entity.AttrAttrgroupRelationEntity;
import com.atyoung.youngmall.product.entity.AttrEntity;
import com.atyoung.youngmall.product.service.AttrAttrgroupRelationService;
import com.atyoung.youngmall.product.service.AttrService;
import com.atyoung.youngmall.product.vo.AttrGroupWithAttrVo;
import com.atyoung.youngmall.product.vo.AttrRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atyoung.common.utils.PageUtils;
import com.atyoung.common.utils.Query;

import com.atyoung.youngmall.product.dao.AttrGroupDao;
import com.atyoung.youngmall.product.entity.AttrGroupEntity;
import com.atyoung.youngmall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrGroupService attrGroupService;
    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catId) {
        QueryWrapper<AttrGroupEntity> qw = new QueryWrapper<>();
        IPage<AttrGroupEntity> page = null;
        if(catId != 0){
            qw.eq("catelog_id",catId);
        }
        if(! StringUtils.isEmpty(params.get("key"))){
            qw.and(obj -> {
                obj.eq("attr_group_id",params.get("key")).or().like("attr_group_name",params.get("key"));
            });
        }
        page = this.page(new Query<AttrGroupEntity>().getPage(params),qw);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> queryAttrRelation(Long attrgroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> qw = new QueryWrapper<AttrAttrgroupRelationEntity>();
        qw.eq("attr_group_id",attrgroupId);
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(qw);
        List<Long> attrIdList = list.stream().map((attr) -> {
            Long attrId = attr.getAttrId();
            return attrId;
        }).collect(Collectors.toList());

        if(attrIdList == null || attrIdList.size() == 0){
            return null;
        }
        Collection<AttrEntity> attrEntities = attrService.listByIds(attrIdList);
        return (List<AttrEntity>)attrEntities;
    }

    @Override
    public void deleteAttrRelation(List<AttrRelationVo> attrRelationVo) {
        attrAttrgroupRelationDao.deleteAttrRelationBatch(attrRelationVo);
    }

    @Override
    public PageUtils queryAttrNoRelation(Long attrgroupId, Map<String, Object> params) {
        return null;
    }

    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> groupEntityList = attrGroupService.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));
        List<AttrGroupWithAttrVo> list = groupEntityList.stream().map(entity -> {
            AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(entity,attrGroupWithAttrVo);
            List<AttrEntity> attrEntitys = attrService.getRelationAttr(entity.getAttrGroupId());
            attrGroupWithAttrVo.setAttrs(attrEntitys);
            return attrGroupWithAttrVo;
        }).collect(Collectors.toList());
        return list;
    }


}